package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;

import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;
    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName",userName);
        qw.eq("u_pwd",pwd);

        UserBean user = mapper.selectOne(qw);
        return user;
    }

    @Override
    public boolean addUser(UserBean user) {

        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName",user.getUserName());

        UserBean tempUser = mapper.selectOne(qw);
        if(tempUser != null){
            return false;
        }
        mapper.insert(user);
        return true;
    }

//    @Override
//    public boolean checkName(String userName) {
//        return mapper.checkName(userName);
//    }

    @Override
    public UserBean findById(Integer userId) {

        return mapper.selectById(userId);
    }

    @Override
    public List<UserBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public boolean updatePwd(String oldPwd,Integer userId, String newPwd) {
       UserBean user= mapper.selectById(userId);
       user.setUserId(userId);
       user.setPwd(newPwd);
       if (oldPwd.equals(user.getPwd())){
           mapper.updateById(user);
           return true;
       }else {
           return false;
       }

    }

    @Override
    public void updateGrade(Integer userId, String grade) {
       UserBean user = new UserBean();
       user.setUserId(userId);
       user.setGrade(grade);
       mapper.updateById(user);
    }

    @Override
    public boolean del(Integer userId, UserBean loginUser) {
        if (userId==loginUser.getUserId()){
            return false;
        }else {
            mapper.deleteById(userId);
            return true;
        }
    }
}
